import 'dart:async';

import 'package:flutter/material.dart';
import "package:get/get.dart";

import '../../common.dart';

RxInt currentCount = 0.obs; // 当前计数器值（虽然在这个示例中不会改变）
late TextEditingController _controller2;
late TextEditingController _controller;

class AddModule extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<AddModule> {
  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  final type = Get.parameters['type'];
  final id = Get.parameters['id'];

  getList() {
    final String url;
    if (type == '1') {
      url = '/bbs/editWiki';
    } else {
      url = '/bbs/addWiki';
    }
    postData(
      url: url,
      data: {
        "title": _controller2.text,
        "text": _controller.text,
        'id': Get.parameters['id'] ?? '',
      },
    ).then((res) {
      if (res != null) {
        showTost('成功');
        Timer(Duration(seconds: 1), () {
          Get.back(result: true);
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    _controller.text = Get.parameters['text'] ?? '';

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xffffffff),
        leadingWidth: 40,
        leading: IconButton(
          onPressed: () {
            Get.back(result: false);
          },
          icon: Image.asset('static/images/back.png'),
        ),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: [
            InputFieldWithCounter(),
            Expanded(
              child: TextField(
                controller: _controller,
                maxLines: null,
                keyboardType: TextInputType.multiline,
                decoration: InputDecoration(
                  hintStyle: TextStyle(color: Color(0xff999999), fontSize: 12),
                  border: InputBorder.none, // 移除边框
                  hintText: '添加内容',
                ),
              ),
            ),
            Expanded(child: Container()),
            Row(
              children: [
                if (type == '1')
                  Expanded(
                    child: GestureDetector(
                      onTap:
                          () => {
                            Get.dialog(
                              AlertDialog(
                                backgroundColor: Color(0xffffffff),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                                content: Container(
                                  height: 140,
                                  child: Column(
                                    children: [
                                      SizedBox(height: 25),

                                      Text(
                                        '您的动态将被永久删除，无法找回，',
                                        style: TextStyle(fontSize: 12),
                                      ),
                                      SizedBox(height: 5),

                                      Text(
                                        '确认删除?',
                                        style: TextStyle(fontSize: 12),
                                      ),
                                      SizedBox(height: 30),
                                      Row(
                                        children: [
                                          Expanded(
                                            child: GestureDetector(
                                              onTap: () {
                                                Get.back();
                                                postData(
                                                  url: '/bbs/delWiki',
                                                  data: {"id": '$id'},
                                                ).then((res) {
                                                  print(666);
                                                  Get.back(result: true);

                                                  showTost('删除成功');
                                                });
                                              },
                                              child: Container(
                                                width: 80,
                                                height: 35,
                                                alignment: Alignment.center,
                                                decoration: BoxDecoration(
                                                  color: Color(0XFFF5F5F5),
                                                  borderRadius:
                                                      BorderRadius.circular(35),
                                                ),
                                                child: Text(
                                                  '确认删除',
                                                  style: TextStyle(
                                                    fontSize: 12,
                                                    color: Color(0XFFEF0000),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),

                                          SizedBox(width: 15),
                                          Expanded(
                                            child: GestureDetector(
                                              onTap: () {
                                                Get.back();
                                              },
                                              child: Container(
                                                width: 80,
                                                height: 35,
                                                alignment: Alignment.center,
                                                decoration: BoxDecoration(
                                                  color: Color(0XFFF5F5F5),
                                                  borderRadius:
                                                      BorderRadius.circular(35),
                                                  gradient: LinearGradient(
                                                    colors: [
                                                      Color(0xff2189E7),
                                                      Color(0xffBF67F8),
                                                    ],
                                                    begin:
                                                        Alignment
                                                            .topLeft, // 渐变开始的位置
                                                    end:
                                                        Alignment
                                                            .bottomRight, // 渐变结束的位置
                                                  ),
                                                ),
                                                child: Text(
                                                  '取消',
                                                  style: TextStyle(
                                                    fontSize: 12,
                                                    color: Color(0XFFFFFFFF),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          },
                      child: Container(
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Color(0XFFF5F5F5),
                          borderRadius: BorderRadius.circular(40),
                        ),
                        height: 40,
                        child: Text('删除', style: TextStyle(fontSize: 16)),
                      ),
                    ),
                  ),
                SizedBox(width: 20),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      // if(type=='1'){
                      //   getList()
                      // }else{
                      //    getList()
                      // }

                      if (_controller2.text.isEmpty) {
                        showTost('标题不为空');
                        return;
                      }
                      if (_controller.text.isEmpty) {
                        showTost('内容不为空');
                        return;
                      }

                      getList();
                    },
                    child: Container(
                      height: 40,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(40),
                        gradient: LinearGradient(
                          colors: [
                            Color(0xff2189E7),
                            Color(0xffBF67F8),
                          ], // 定义颜色列表
                          begin: Alignment.topLeft, // 渐变开始的位置
                          end: Alignment.bottomRight, // 渐变结束的位置
                        ),
                      ),
                      child: Text(
                        '完成',
                        style: TextStyle(
                          fontSize: 16,
                          color: Color(0XFFFFFFFF),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 20),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

class InputFieldWithCounter extends StatefulWidget {
  @override
  _InputFieldWithCounterState createState() => _InputFieldWithCounterState();
}

class _InputFieldWithCounterState extends State<InputFieldWithCounter> {
  @override
  void initState() {
    super.initState();
    _controller2 = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    _controller2.text = Get.parameters['title'] ?? '';
    return Container(
      margin: EdgeInsets.only(top: 10),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(color: Color(0xffdddddd))),
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              onChanged: (value) {
                currentCount.value = value.length;
              },
              maxLength: 10,
              controller: _controller2,
              decoration: InputDecoration(
                counterText: '',
                hintStyle: TextStyle(color: Color(0xff999999), fontSize: 12),
                border: InputBorder.none, // 移除边框
                hintText: '添加模块标题',
              ),
            ),
          ),
          SizedBox(width: 8),
          Obx(
            () => Text(
              '${currentCount}/10',
              style: TextStyle(color: Colors.grey),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _controller2.dispose();
    super.dispose();
  }
}
